import SwiftUI

// 创建 Router 对象
@Observable final class Router {
    var path = NavigationPath()

    // 路由推送
    func push<T: Hashable>(_ value: T) {
        path.append(value)
    }

    // 路由返回
    func pop() {
        if !path.isEmpty {
            path.removeLast()
        }
    }

    // 返回到根视图
    func reset() {
        path.removeLast(path.count)
    }
}

// 定义导航目标
enum AppRoute: Hashable {
    case foodDetailView(storeId: Int)
    case storeDetailView(storeId: Int)
    case searchFoodStoreView(search: String)
    
    func hash(into hasher: inout Hasher) {
        // 只哈希枚举的类型,忽略关联值
        switch self {
        case .foodDetailView:
            hasher.combine(1)
        case .storeDetailView:
            hasher.combine(2)
        case .searchFoodStoreView:
            hasher.combine(3)
        }
    }
    
    static func == (lhs: AppRoute, rhs: AppRoute) -> Bool {
        switch (lhs, rhs) {
        case (.foodDetailView, .foodDetailView):
            return true
        case (.storeDetailView, .storeDetailView):
            return true
        case (.searchFoodStoreView, .searchFoodStoreView):
            return true
        default:
            return false
        }
    }
}
